<?php
class SmsModel extends Model
{         
	public function makeCode()
	{
		return rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9);
	}
	public function demo()
	{
		
		
	}
	public function addInfo($tel, $type, $code, $content, $status)
	{
		$arr = array("telnum" => $tel, "type" => $type, "code" => $code, "content" => $content, "send_time" => time(), "status" => $status, "isuse" => 0);
		return $this->add($arr);
	}
	public function isOften($tel, $type)
	{
		$result = $this->where(array("telnum" => $tel, "type" => $type, "status" => 1))->order("send_time Desc")->find();
		if ($result && time() - $result["send_time"] < 300) {
			return true;
		}
		return false;
	}
	public function getInfo($tel, $type)
	{
		$result = $this->where(array("telnum" => $tel, "type" => $type))->order("send_time Desc")->find();
		if (!$result) {
			return false;
		}
		if ($result["isuse"]) {
			$this->where(array("id" => $result["id"]))->save(array("isuse" => 1));
		}
		return $result;
	}
	public function getInfoMy($tel, $code,$type)
	{
		$result = $this->where(array("telnum" => $tel,"code"=>$code, "type" => $type))->order("send_time Desc")->find();
		if (!$result) {
			return false;
		}
		if ($result["isuse"]) {
			$this->where(array("id" => $result["id"]))->save(array("isuse" => 1));
		}
		return $result;
	}	
	public function SSend($mobile, $content,$code=0,$type='')
	{
	    $url="http://api2.santo.cc/submit?command=MT_REQUEST&cpid=1778788789&cppwd=123457&da=".$mobile."&sm=".'【'.C("sms_name").'】'.$content;
	//	$url="http://api.sms.cn/sms/?ac=send&uid=".C('sms_user')."&pwd=".md5(C('sms_password'))."&mobile=".$mobile."&content=".urlencode('【'.C("sms_name").'】'.$content);
	 
		$Content=curl($url, array(), 0);
		echo $Content;
	}
	 public function qSend($mobile, $content,$code=0,$type='')
	 {
	$url="http://www.g106.cn/sms.aspx?action=send";
	 // $url="http://api.sms.cn/sms/?ac=send&uid=用户账号&pwd=MD532位密码&mobile=号码&content=内容";
	 $post_data = [];
	 //$post_data['action'] = "send";
     $post_data['userid'] =C('sms_extno');
     $post_data['account'] = C('sms_user');
     $post_data['password'] = C('sms_password');

     $post_data['mobile'] =$mobile;
    // $post_data['timestamp'] =date("YmdHis");
   //  $post_data['sign'] = md5("zengyouqianaa123456".date("YmdHis"));
     $content=str_replace(array("\r\n", "\r", "\n"), "", $content);
     $post_data['content']='【'.C("sms_name").'】'.$content;
     $post_data['sendTime']="";
    // $post_data['extno']="";
 
 
		$result = $this->curl_post($url, $post_data);
		$phone = $number;//要发送短信的手机号码
 
	  $result = json_decode(json_encode((array) simplexml_load_string($result)), true);
	   
		 $status = 0;
	 	if($result['returnstatus'] == 'Success'){
             $status = 1;
	 	}
	 	 
	     $arr = array("telnum" => $mobile, "type" => $type, "code" =>$code, "content" => $content, "send_time" => time(), "status" => $status, "isuse" => 1,'msg'=>$result['message']);
		$this->add($arr);



  
		return array("status" => $status, "mess" => $result['message']);
	 }
	//发送验证码
	public function sendSms($mobile, $content,$code=0,$type=''){
			return $this->sms5c($mobile, $content,$code,$type);
	     return $this->smssmstcby($mobile, $content,$code,$type);
		 return $this->v3Sms($mobile, $content,$code,$type);
	    return $this->SSend($mobile, $content,$code,$type);
	    if($mobile=="13838383838"){
		 return $this->SSend($mobile, $content,$code,$type);
	    }else{
         return $this->qSend($mobile, $content,$code,$type);
	    }
		 return $this->v2Sms($mobile, $content,$code,$type);
    }
	public function sms5c($mobile, $content,$code=0,$type=''){
		
			$url="http://m.5c.com.cn/api/send/index.php";
			if(strpos($content,"证码为")== false){
				 $content='【'.C("sms_name").'】'."您的订单已通过";
			}else{
				$content= '【'.C("sms_name").'】'.$content; //短信内容
			}
			 
			 
		   
			$post_data = array();
           
			$post_data['username']="18488348120";
			$post_data['password_md5']=md5("asdf1234");
			 
            $post_data['apikey'] ="4fe772f1b95e86a4d9eafee27067f31f";
           
            $post_data['content'] =urlencode($content);
            $post_data['mobile'] = $mobile;
            $post_data['encode'] = 'UTF8'; 
	 	$result = $this->curl_post($url, $post_data,array("X-FORWARDED-FOR:182.61.173.245","CLIENT-IP:182.61.173.245"));
		 
		$phone = $number;//要发送短信的手机号码
	 
	 // $result = json_decode(json_encode((array) simplexml_load_string($result)), true);
	   
		 $status = 0;
	 	if(strpos($result,"uccess")!== false ){
             $status = 1;
	 	}
	 	 
	     $arr = array("telnum" => $mobile, "type" => $type, "code" =>$code, "content" => $content, "send_time" => time(), "status" => $status, "isuse" => 1,'msg'=>$result);
		$this->add($arr);



  
		return array("status" => $status, "mess" => $result['message']);
			
	}
	public function smssmstcby($mobile, $content,$code=0,$type=''){
		 
		   $content= '【'.C("sms_name").'】'.$content; //短信内容
		    $content="【TP】您的订单已通过";
			$post_data = array();
            $post_data['type'] ="send";
			$post_data['username']="2863074379";
			$post_data['password']=md5("2863074379");
			 
            $post_data['gwid'] ="d536f166";
           
            $post_data['message'] =$content;
            $post_data['mobile'] = $mobile;
            $post_data['rece'] = 'json'; 
            $url='http://jk.smstcby.com/smsUTF8.aspx';
            $result = $this->curl_post($url, $post_data,array("X-FORWARDED-FOR:182.61.173.245","CLIENT-IP:182.61.173.245"));
            
            $result=(array)json_decode($result);
             
             $status = 0;
	 	if($result['returnstatus'] == 'success'){
             $status = 1;
	 	}
	 	 
	     $arr = array("telnum" => $mobile, "type" => $type, "code" =>$code, "content" => $content, "send_time" => time(), "status" => $status, "isuse" => 1,'msg'=>$result['remark']);
		$this->add($arr);



  
		return array("status" => $status, "mess" => $result['remark']);
		    echo $result;
	}
    public function v3Sms($mobile, $content,$code=0,$type=''){
       
            $post_data = array();
            $post_data['userid'] = C('sms_extno');
            $post_data['account'] = C('sms_user');
            $post_data['password'] = C('sms_password');
            $post_data['content'] = '【'.C("sms_name").'】'.$content; //短信内容
            $post_data['mobile'] = $mobile;
            $post_data['sendtime'] = ''; //时定时发送，输入格式YYYY-MM-DD HH:mm:ss的日期值
            $url='http://39.99.239.106:8888/sms.aspx?action=send';
            $result = $this->curl_post($url, $post_data);
              $result = json_decode(json_encode((array) simplexml_load_string($result)), true);
	     
		 $status = 0;
	 	if($result['returnstatus'] == 'Success'){
             $status = 1;
	 	}
	 	 
	     $arr = array("telnum" => $mobile, "type" => $type, "code" =>$code, "content" => $content, "send_time" => time(), "status" => $status, "isuse" => 1,'msg'=>$result['message']);
		$this->add($arr);



  
		return array("status" => $status, "mess" => $result['message']);
        }
	public  function v2Sms($mobile, $content,$code=0,$type=''){
		$url="http://39.99.239.106:8888/v2sms.aspx";
	 
	 $post_data = [];
	 $timestamp=date("YmdHis");
	 //$post_data['action'] = "send";
     $post_data['userid'] =C('sms_extno');
     $post_data['timestamp']=$timestamp;//C('sms_user');
     $post_data['sign'] =md5(C('sms_user').C('sms_password').$timestamp);// C('sms_password');

     $post_data['mobile'] =$mobile;
   
     $content=str_replace(array("\r\n", "\r", "\n"), "", $content);
     $post_data['content']='【'.C("sms_name").'】'.$content;
     $post_data['sendTime']="";
    $post_data['action']="send";
	 $post_data['extno']="";
 
 
		$result = $this->curl_post($url, $post_data);
		$phone = $number;//要发送短信的手机号码

  
	  $result = json_decode(json_encode((array) simplexml_load_string($result)), true);
	     
		 $status = 0;
	 	if($result['returnstatus'] == 'Success'){
             $status = 1;
	 	}
	 	 
	     $arr = array("telnum" => $mobile, "type" => $type, "code" =>$code, "content" => $content, "send_time" => time(), "status" => $status, "isuse" => 1,'msg'=>$result['message']);
		$this->add($arr);



  
		return array("status" => $status, "mess" => $result['message']);
	}
 
	/**
     * CURL POST请求方法,默认header为JSON格式
     * @param $url  string  请求路径
     * @param array $data   请求参数JSON数组,默认为空
     * @return mixed|string
     */
    private function curl_post($url,$data = [],$header = []) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        if($header) {
            curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
        }
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            return curl_error($ch);
        }
        curl_close($ch);
        return $result;
    }
}